package com.example.javatest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javatest.entity.domain.TbUser;
import com.example.javatest.service.TbUserService;
import com.example.javatest.mapper.TbUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.example.javatest.constant.UserConstant.USER_LOGIN_STATE;

/**
* @author cac_qin
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2023-12-06 16:03:08
*/
@Service
@Slf4j
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Resource
    private TbUserMapper tbUserMapper;

    private static final String SALT="cac";



    @Override
    public int userRegister(String userName, String userPwd, String checkPassword) {

        if(StringUtils.isAnyBlank(userName,userPwd,checkPassword)){
            return -1;
        }
        if(userName.length()<2){
            return  -1;
        }
        if(userPwd.length()<8 || checkPassword.length()<8){
            return -1;
        }

        //客户不能包含特殊字符
        String validPatten="^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$";
        Matcher matcher = Pattern.compile(validPatten).matcher(userName);
        if(matcher.find()){
            return -1;
        }
        if(!userPwd.equals(checkPassword)){
            return -1;
        }
        //账户是否重复
        QueryWrapper<TbUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_Name",userName);
        int count = tbUserMapper.selectCount(queryWrapper);
        if(count>0){
            return -1;
        }
        //对密码进行加密
//        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+userPwd).getBytes());

        //插入数据
        TbUser tbUser =new TbUser();
        tbUser.setUserName(userName);
        tbUser.setUserPwd(userPwd);
        tbUser.setClassId(1000);
        boolean saveResult = this.save(tbUser);
        if(!saveResult){
            return -1;
        }
        return tbUser.getUserId();
    }

    @Override
    public TbUser userLogin(String userName, String userPwd, HttpServletRequest request) {
        if(StringUtils.isAnyBlank(userName,userPwd)){
            return null;
        }
        if(userName.length()<2){
            return null;
        }
        if(userPwd.length()<8 ){
            return null;
        }

        //客户不能包含特殊字符
        String validPatten="^.*[/^/$/.//,;:'!@#%&/*/|/?/+/(/)/[/]/{/}]+.*$";
        Matcher matcher = Pattern.compile(validPatten).matcher(userName);
        if(matcher.find()){
            return null;
        }
        //对密码进行加密
//        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+userPwd).getBytes());

        //查询数据库
        QueryWrapper<TbUser> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_Name",userName);
        queryWrapper.eq("user_Pwd",userPwd);
        TbUser tbUser = tbUserMapper.selectOne(queryWrapper);

        //用户不存在
        if(tbUser == null) {
            log.info("user login failed,userName cannot match userPwd");
            return null;
        }
        //3.用户脱敏
        TbUser safetyUser = new TbUser();
        safetyUser.setUserId(tbUser.getUserId());
        safetyUser.setUserName(tbUser.getUserName());
        safetyUser.setClassId(tbUser.getClassId());
        safetyUser.setUserIdentity(tbUser.getUserIdentity());

        //2.记录用户的登录态
        request.getSession().setAttribute(USER_LOGIN_STATE,safetyUser);

        System.out.println(safetyUser);
        return safetyUser;
    }
}




